import { Node } from 'cc';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { UIID } from '../../../common/config/GameUIConfig';
import { Load } from '../load/Load';
import { SceneEntity } from '../SceneEntity';
import { LevelLayoutComp } from './bll/LevelLayoutSystem';
import { LevelModelComp } from './model/LevelModelComp';
import { LevelViewComp } from './view/LevelViewComp';
import { LevelMap } from '../../biz/level-map/LevelMap';
import { GameCamera } from '../../biz/game-camera/GameCamera';

/** 关卡 */
@ecs.register('Level')
export class Level extends SceneEntity {
  // 数据层
  LevelModel!: LevelModelComp; // 关卡模型

  // 视图层

  // 实例
  GameCamera!: GameCamera; // 摄像机实例
  LevelMap!: LevelMap; // 关卡地图实例
  init() {
    // 1.添加摄像机实例
    this.GameCamera = ecs.getEntity<GameCamera>(GameCamera);
    this.addChild(this.GameCamera);

    this.addComponents(LevelModelComp);
  }
  /** 初始化布局 */
  initLayout(): void {
    // 添加初始化布局组件
    this.add(LevelLayoutComp);
  }

  /** 跳转到游戏关卡 */
  goto() {
    const load = ecs.getEntity<Load>(Load);
    load.loadRes(
      this,
      UIID.Level,
      [
        { text: '正在加载游戏资源...', path: 'prefab' },
        { text: '正在加载游戏资源...', path: 'images' },
      ],
      this.jumpCompleted.bind(this)
    );
  }

  /** 加载跳转完成 */
  jumpCompleted(node: Node) {
    const mv = node.addComponent(LevelViewComp)!;
    this.add(mv);
    mv.loadUINode(this.LevelModel.uiNode);
  }
}
